import Vue from 'vue'
import Router from 'vue-router'
import lazyLoading from '@/utils/lazyLoading'
import { getToken } from '@/utils/auth' // getToken from cookie
Vue.use(Router)

// 定义路由节点
const routes = [
  { 
    path: '*',
    component: lazyLoading(404)
  },
  {
    path: '',
    redirect: to => {
      return '/login'
    }
  },
  {
    path: '/login',
    name: '登陆页',
    component: lazyLoading('Login'),
    meta: {
    }
  },
  {
    path: '/',
    component: lazyLoading('AppMain'),
    meta: {
    },
    children: [
      {
        path: '/businessdetails',
        name: "业务明细",
        component: lazyLoading('businessDetails', 'details', true)
      },
      // 账号管理
      {
        path: '/account',
        name: "账号管理",
        component: lazyLoading('account', 'account', true)
      },
      // 用户管理
      {
        path: '/user',
        name: "用户管理",
        component: lazyLoading('user', 'user', true)
      },
      // 产品管理
      {
        path: '/product',
        component: lazyLoading('productManager', 'product', true),
        children: [
          {
            path: "",
            redirect: to => {
              return "/product/config"
            }
          },
          {
            path: 'config',
            name: '产品配置',
            component: lazyLoading('productManager/productConfig', 'product', true),
            meta: {
            }
          },
          {
            path: 'banners',
            name: 'Banner推荐',
            component: lazyLoading('productManager/banners', 'product', true),
            meta: {
            }
          },
          {
            path: 'orders',
            name: '产品排序',
            component: lazyLoading('productManager/productOrders', 'product', true),
            meta: {
            }
          },
          {
            path: 'settlement',
            name: '产品结算',
            component: lazyLoading('productManager/productSettlement', 'product', true),
            meta: {
            }
          }
        ]
      },
      // 渠道管理
      {
        path: '/channel',
        component: lazyLoading('channel', 'channel', true),
        children: [
          {
            path: "",
            redirect: to => {
              return "/channel/channelconfig"
            }
          },
          {
            path: 'channelconfig',
            name: '渠道配置',
            component: lazyLoading('channel/channelConfig', 'channel', true),
            meta: {
            }
          },
          {
            path: 'statistics',
            name: '渠道统计',
            component: lazyLoading('channel/statistics', 'channel', true),
            meta: {
            }
          },
          {
            path: 'channelSettlement',
            name: '渠道结算',
            component: lazyLoading('channel/channelSettlement', 'channel', true),
            meta: {
            }
          },
          {
            path: 'JPCheck',
            name: '截屏监测',
            component: lazyLoading('channel/JPCheck', 'channel', true),
            meta: {
            }
          },
          {
            path: 'IPCheck',
            name: 'IP监测',
            component: lazyLoading('channel/IPCheck', 'channel', true),
            meta: {
            }
          },
          {
            path: 'personSave',
            name: '七日留存',
            component: lazyLoading('channel/personSave', 'personSave', true),
            meta: {
            }
          }
        ]
      },
      {
        path: '/statistics',
        component: lazyLoading('statistics', 'statistics', true),
        children: [
          {
            path: "",
            redirect: to => {
              return "/statistics/daichao"
            }
          },
          {
            path: 'daichao',
            name: '贷超转化',
            component: lazyLoading('statistics/daichao', 'statistics', true),
            meta: {
            }
          },
          {
            path: 'position',
            name: '位置转化',
            component: lazyLoading('statistics/position', 'statistics', true),
            meta: {
            }
          }
        ]
      },
      {
        path: '/finance',
        component: lazyLoading('finance', 'finance', true),
        children: [
          {
            path: "",
            redirect: to => {
              return "/finance/dailyStatistics"
            }
          },
          {
            path: 'financeFlow',
            name: '财务流水',
            component: lazyLoading('finance/financeFlow', 'finance', true),
            meta: {
            }
          },
          {
            path: 'dailyStatistics',
            name: '每日统计',
            component: lazyLoading('finance/dailyStatistics', 'finance', true),
            meta: {
            }
          }
        ]
      },
      {
        path: '/complaint',
        name: "投诉列表",
        component: lazyLoading('complaint', 'complaint', true)
      },
      {
        path: '/app',
        component: lazyLoading('app', 'app', true),
        children: [
          {
            path: "",
            redirect: to => {
              return "/app/config"
            }
          },
          {
            path: 'config',
            name: 'app配置',
            component: lazyLoading('app/config', 'config', true),
            meta: {
            }
          },
          {
            path: 'appVersion',
            name: 'app版本',
            component: lazyLoading('app/appVersion', 'app', true),
            meta: {
            }
          }
        ]
      },
      {
        path: '/operation',
        component: lazyLoading('operation', 'operation', true),
        children: [
          {
            path: "",
            redirect: to => {
              return "/operation/miScore"
            }
          },
          {
            path: 'miScore',
            name: '米分列表',
            component: lazyLoading('operation/miScore', 'operation', true),
            meta: {
            }
          }
        ]
      }
    ]
  }
]
// 创建路由router实例
const router = new Router({
  routes
})

// 路由白名单
const whiteList = ['/login']
// 路由导航(路由拦截器)
router.beforeEach((to, from, next) => {
  if (getToken()) {
    /* has token*/
    if (to.path === '/login') {
      if(localStorage.getItem('SYSTEM_ROLE') === 'SUPER_ADMIN'){
        next({ path: '/account' })
      } else if(localStorage.getItem('SYSTEM_ROLE') === 'CHANNEL_ADMIN'){
        next({ path: '/businessdetails' })
      } else if(localStorage.getItem('SYSTEM_ROLE') === 'STAFF_CHANNEL'){
        next({ path: '/channelconfig' })
      } else{
        next({ path: '/product' })
      }
    } else {
      if(localStorage.getItem('SYSTEM_ROLE') === 'CHANNEL_ADMIN' && to.path !== '/businessdetails'){
        next({ path: '/businessdetails' })
      }else{
        next()
      }
      // if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
      //   store.dispatch('GetInfo').then(res => { // 拉取user_info
      //     const roles = res.roles // note: roles must be a array! such as: ['editor','develop']
      //     store.dispatch('GenerateRoutes', { roles }).then(() => { // 根据roles权限生成可访问的路由表
      //       router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
      //       next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
      //     })
      //   }).catch((err) => {
      //     store.dispatch('FedLogOut').then(() => {
      //       Message.error(err || 'Verification failed, please login again')
      //       next({ path: '/' })
      //     })
      //   })
      // } else {
      //   next()
      // }
    }
  } else {
    /* has no token*/
    if (whiteList.indexOf(to.path) !== -1) { // to.path='/login',在免登录白名单中,直接进入该路由页面
      next()
    } else {
      next({ path: '/login' }) // 不在白名单中,全部重定向到登录页
    }
  }
})

export default router
